<template>
  <div class="wrap">
    <!-- 播放器主体区域 -->
    <div class="play_wrap" id="player">
      <!-- 搜索歌曲 -->
      <div class="search_bar">
        <input
          type="text"
          autocomplete="off"
          v-model="keywords"
          @keyup.enter="flag && seachMusic()"
        />
        <button @click="prev" class="prev">上一首</button>
        <button @click="next" class="next">下一首</button>
      </div>

      <div class="center_con">
        <!-- 所有歌曲列表 -->
        <div class="song_wrapper">
          <ul class="song_list">
            <li
              v-for="(item,index) in musicList"
              :key="item.id"
              @click="playMusic(item.id,index)"
              :class="{active:index===currentIndex}"
            >
              <!-- 播放按钮 -->
              <a href="javascript:;"></a>
              <!-- 歌曲名字 -->
              <b>{{item.name}}</b>
              <!-- mv图标播放 -->
              <span v-if="item.mv!=0" @click.stop="playMv(item)">
                <i></i>
              </span>
              <!---->
            </li>
          </ul>
          <img src="../images/line.png" class="switch_btn" alt />
        </div>
        <!-- 歌曲信息容器 -->
        <div class="player_con" :class="{playing:isplaying}">
          <img src="../images/player_bar.png" class="play_bar" />
          <!-- 黑胶碟片 -->
          <img src="../images/disc.png" class="disc autoRotate" />
          <img :src="musicPic" class="cover autoRotate" />
        </div>
        <!-- 评论容器 -->
        <div class="comment_wrapper">
          <h5 class="title">热门留言</h5>
          <div class="comment_list" v-if="musciComments && musciComments.length >0">
            <dl v-for="comment in musciComments" :key="comment.commentId">
              <!-- 评论人头像 -->
              <dt>
                <img :src="comment.user.avatarUrl" alt />
              </dt>
              <!-- 评论人的名字 -->
              <dd class="name">{{comment.user.nickname}}</dd>
              <!-- 评论人内容 -->
              <dd class="detail">{{comment.content}}</dd>
            </dl>
          </div>
          <img src="../images/line.png" class="right_line" />
        </div>
      </div>
      <!-- 音乐播放 -->
      <div class="audio_con">
        <audio
          ref="audio"
          @play="play"
          @pause="pause"
          :src="musicUrl"
          controls
          autoplay
          loop
          class="myaudio"
        ></audio>
      </div>
      <!-- mv显示 -->
      <div class="video_con" v-show="isShow" style="display: none;">
        <video :src="mvUrl" controls="controls"></video>
        <div class="mask" @click="hide"></div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "Music",
  data() {
    return {
      currentIndex: -1,
      isplaying: false, //是否正在播放
      isMvShow: false, //mv图标false默认不显示
      keywords: "", //搜索内容
      musicList: [], //所有歌曲列表
      musicPic: "", //歌曲图片
      musciComments: [], //音乐热门评论
      musicUrl: "", //音乐播放地址
      isShow: false, //mv默认不显示
      mvUrl: "", //mv的url地址
      flag: true //用户控制用户点击回车键是否可以再点击(1.3s)
    };
  },
  created() {
    // 创建完成
    // 获取所有歌曲列表
    this.getSongsList();
  },
  methods: {
    // 获取所有歌曲列表

    async getSongsList() {
      try {
        //  让异步代码同步化
        const res = await this.$axios.get(
          "http://localhost:3000/artist/top/song?id=6452"
        );
        this.musicList = res.data.songs;
      } catch (error) {
        console.log(error);
      }
    },

    //  播放音乐
    async playMusic(id, index) {
      this.currentIndex = index;
      const res = await this.$axios.get("http://localhost:3000/song/url?id=" + id);
      //console.log(res);
      this.musicUrl = res.data.data[0].url;
      this.getPic(id);
      this.getMusciComments(id);
    },
    async getPic(id) {
      const res = await this.$axios.get(
        "http://localhost:3000/song/detail?ids=" + id
      );
      this.musicPic = res.data.songs[0].al.picUrl;
    },
    async getMusciComments(id) {
      const res = await this.$axios.get(
        "http://localhost:3000/comment/hot?type=0&id=" + id
      );
      this.musciComments = res.data.hotComments;
    },
    // 搜索歌曲
    async seachMusic() {
      if (this.keywords) {
        const res = await this.$axios.get(
          `http://localhost:3000/search/?keywords=${this.keywords}`
        );
        this.musicList = res.data.result.songs;
        this.keywords = "";
        console.log(res.data.result.songs);
      } else {
        this.flag = false;
        setTimeout(() => {
          this.flag = true;
          this.getAllMusicList();
        }, 1300);
      }
    },
    // 上一首
    prev() {
      this.currentIndex--;
      if (this.currentIndex == -1) {
        this.currentIndex = this.musicList.length - 1;
      }
      const id = this.musicList[this.currentIndex].id;
      this.playMusic(id, this.currentIndex);
    },
    // 下一首
    next() {
      this.currentIndex++;
      if (this.currentIndex == this.musicList.length) {
        this.currentIndex = 0;
      }
      const id = this.musicList[this.currentIndex].id;
      this.playMusic(id, this.currentIndex);
      //this.scrollList()
    },
    scrollList() {
      let pos = document.querySelector(".song_list .active").offsetTop + 30;
      document.querySelector(".song_list").scrollTo(0, pos);
    },
    // 音乐播放
    play() {
      this.isplaying = true;
      console.log("播放中。。。");
    },
    // 音乐暂停
    pause() {
      this.isplaying = false;
      console.log("音乐暂停了！");
    },
    // 播放mv
    async playMv(item) {
      let url = item.mv
        ? `http://localhost:3000/mv/url?id=${item.mv}`
        : `http://localhost:3000/mv/url?id=${item.mvid}`;
      const r = await this.$axios.get(url);
      this.mvUrl = r.data.data.url;
      this.isShow = true;
      this.musicUrl = "";
      this.isplaying = false;
    },
    // 点击遮罩层 隐藏mv
    hide() {
      this.isShow = false;
      this.mvUrl = "";
    }
  }
};
</script>

<style scope>
@import "../css/index.css";
</style>